<template>
  <Modal :title="name==0 ? '创建普通课程' : '创建案例联席课程'"
         id="user-management-add-update"
         width="600px"
         :mask-closable="false"
         :footer-hide="true"
         v-model="visible">
    <create-live-course-steps v-if="name=='0'"
                              @createCourseDone="createCourseDone"></create-live-course-steps>
    <create-case-course-steps v-else
                              @createCourseDone="createCourseDone"></create-case-course-steps>
  </Modal>
</template>

<script>
import CreateLiveCourseSteps from './create-live-course-steps'
import CreateCaseCourseSteps from './create-case-course-steps'
export default {
  data () {
    return {
      visible: false,
      dataForm: {},
      name: ''
    }
  },
  components: {
    CreateLiveCourseSteps,
    CreateCaseCourseSteps
  },
  methods: {
    init (name, row) {
      this.name = name
      this.visible = true
      this.$nextTick(() => {
        if (row) {
          this.addNew = false
          this.dataForm = row
          this.dataForm['passwdCheck'] = row.password
        } else {
          this.dataForm = {
            loginName: '',
            roleId: 0,
            no: '',
            password: '',
            roleName: '',
            name: '',
            email: '',
            mobile: '',
            photo: ''
          }
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      if (this.addNew) {
        if (this.uploadList.length > 0) {
          this.$refs['formCustom'].validate(valid => {

            if (valid) {
              let data = new FormData()
              data.append('loginName', this.dataForm.loginName)
              data.append('mf', this.uploadList[0])
              uploadPhoto(data).then(res => {

                if (res.data && res.data != 'fail') {
                  let data = {}
                  for (let key in this.dataForm) {
                    if (key !== 'passwdCheck') {
                      data[key] = this.dataForm[key]
                    }
                  }
                  data['photo'] = res.data
                  addUser(data).then(res => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  })
                }
              })
            }
          })
        } else {
          this.$Message.warning('请选择一张头像')
        }
      } else {

        if (this.uploadList.length > 0) {
          this.$refs['formCustom'].validate(valid => {
            if (valid) {
              let data = new FormData()
              data.append('loginName', this.dataForm.loginName)
              data.append('mf', this.uploadList[0])
              uploadPhoto(data).then(res => {
                if (res.data && res.data != 'fail') {
                  let data = {}
                  for (let key in this.dataForm) {
                    if (key !== 'passwdCheck') {
                      data[key] = this.dataForm[key]
                    }
                  }
                  data['photo'] = res.data
                  updateUser(data).then(res => {

                  })
                }
              })
            }
          })
        } else {

          this.$refs['formCustom'].validate(valid => {
            if (valid) {
              let data = {}
              for (let key in this.dataForm) {
                if (key !== 'passwdCheck') {
                  data[key] = this.dataForm[key]
                }
              }
              updateUser(data).then(res => {

                if (res.data === 'success') {
                  this.$emit('refreshDataList')
                  this.visible = false
                }
              })
            }
          })
        }
      }
    },
    createCourseDone () {
      this.visible = false
    }
  }
}
</script>
<style lang="scss" scoped>
#user-management-add-update {
  .form-custom {
    .ivu-form-item {
      width: 50%;
      float: left;
      .ivu-input {
        width: 200px;
      }
    }
  }
  .footer {
    text-align: right;
  }
}
</style>